package com.zzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.common.exception.ServiceException;
import com.zzx.entity.TGxtype;
import com.zzx.mapper.TGxtypeMapper;
import com.zzx.service.TGxtypeService;
import com.zzx.utils.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author CD
 * @description 针对表【t_gxtype】的数据库操作Service实现
 * @createDate 2024-11-28 17:28:34
 */
@Service
public class TGxtypeServiceImpl extends ServiceImpl<TGxtypeMapper, TGxtype>
        implements TGxtypeService {
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TGxtype param) {
        LambdaQueryWrapper<TGxtype> wrapper = new LambdaQueryWrapper<>(TGxtype.class);
        // 多租户拦截器会自动添加 company_id 条件
        wrapper.eq(TGxtype::getGxtypeName, param.getGxtypeName());
        if (ObjectUtils.isEmpty(param.getGxtypeId())) {
            param.setGxtypeId(IdUtil.getSnowflakeNextId());
        } else {
            wrapper.ne(TGxtype::getGxtypeId, param.getGxtypeId());
            EntityUtils.mergeNonNullProperties(this.getBaseMapper().selectById(param.getGxtypeId()), param);
        }
        List<TGxtype> tGxtypeList = this.getBaseMapper().selectList(wrapper);
        if (!tGxtypeList.isEmpty()) {
            throw new ServiceException("工序类型名称已存在");
        }
        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TGxtype param) {
        this.removeById(param.getGxtypeId());
    }

    @Override
    public List<TGxtype> selectBy(TGxtype param) {
        LambdaQueryWrapper<TGxtype> wrapper = new LambdaQueryWrapper<>(TGxtype.class);
        if (!ObjectUtils.isEmpty(param.getGxtypeName())) {
            wrapper.like(TGxtype::getGxtypeName, param.getGxtypeName());
        }
        if (!ObjectUtils.isEmpty(param.getGxtypeCode())) {
            wrapper.like(TGxtype::getGxtypeCode, param.getGxtypeCode());
        }
        if (!ObjectUtils.isEmpty(param.getGxtypeId())) {
            wrapper.eq(TGxtype::getGxtypeId, param.getGxtypeId());
        }
        // 多租户拦截器会自动添加 company_id 条件
        return this.getBaseMapper().selectList(wrapper);
    }
}




